package LC;

import java.math.BigInteger;
import java.util.Scanner;

/**
 * https://leetcode.com/problems/reverse-integer/description/
 * Reverse digits of an integer.
 * Example1: x = 123, return 321
 * Example2: x = -123, return -321
 * click to show spoilers.
 * Note:
 * The input is assumed to be a 32-bit signed integer.
 * Your function should return 0 when the reversed integer overflows.
 */
public class LC_007_ReverseInteger_JavaAPI {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int n = sc.nextInt();
            System.out.println(reverse(n));
        }
        sc.close();
    }

    private static int reverse(int x) {
        StringBuilder sb = new StringBuilder(String.valueOf(x));
        String rx = x >= 0 ? sb.reverse().toString() : sb.deleteCharAt(0).reverse().toString();
        int n1 = new BigInteger(String.valueOf(Integer.MAX_VALUE)).compareTo(new BigInteger(rx));
        int n2 = new BigInteger(String.valueOf(Integer.MIN_VALUE)).compareTo(new BigInteger(rx));
        if (x < 0 && n1 >= 0 && n2 <= 0)
            return Integer.parseInt("-" + rx);
        else if (x >= 0 && n1 >= 0 && n2 <= 0)
            return Integer.parseInt(rx);
        else
            return 0;
    }
}